<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="en">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="https://fonts.loli.net/css?family=Lato:300,300italic,400,400italic,700,700italic|Lobster:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/favicon.ico?v=5.1.4" color="#222">


  <link rel="manifest" href="/images/manifest.json">




  <meta name="keywords" content="python,game," />










<meta name="description" content="去过赌场的朋友应该见过这个 —- 骰宝（Sic Bo）">
<meta name="keywords" content="python,game">
<meta property="og:type" content="article">
<meta property="og:title" content="赌博小游戏的开发 - 骰宝">
<meta property="og:url" content="http://codewithzhangyi.com/2018/06/13/赌博小游戏的开发-骰宝-py/index.html">
<meta property="og:site_name" content="Zhang Yi">
<meta property="og:description" content="去过赌场的朋友应该见过这个 —- 骰宝（Sic Bo）">
<meta property="og:locale" content="en">
<meta property="og:image" content="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/002.jpg?raw=true">
<meta property="og:image" content="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/003.png?raw=true">
<meta property="og:image" content="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/Sic_bo_Table.png?raw=true">
<meta property="og:image" content="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/010.png?raw=true">
<meta property="og:image" content="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/011.png?raw=true">
<meta property="og:updated_time" content="2019-02-04T07:32:06.909Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="赌博小游戏的开发 - 骰宝">
<meta name="twitter:description" content="去过赌场的朋友应该见过这个 —- 骰宝（Sic Bo）">
<meta name="twitter:image" content="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/002.jpg?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://codewithzhangyi.com/2018/06/13/赌博小游戏的开发-骰宝-py/"/>






<script data-ad-client="ca-pub-2691877571661707" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  <title>赌博小游戏的开发 - 骰宝 | Zhang Yi</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="en">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Zhang Yi</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle" style="color:#fff">
    <button>MENU</button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-projects">
          <a href="/projects/" rel="section">
            
            Projects
          </a>
        </li>
      
        
        <li class="menu-item menu-item-blog">
          <a href="/blog/" rel="section">
            
            Blog
          </a>
        </li>
      
        
        <li class="menu-item menu-item-activity">
          <a href="/activity/" rel="section">
            
            Activity
          </a>
        </li>
      
        
        <li class="menu-item menu-item-list-100">
          <a href="/list-100/" rel="section">
            
            List 100
          </a>
        </li>
      
        
        <li class="menu-item menu-item-friends">
          <a href="/friends/" rel="section">
            
            Friends
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
            Search
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="Searching..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://codewithzhangyi.com/2018/06/13/赌博小游戏的开发-骰宝-py/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="ZhangYi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Zhang Yi">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">赌博小游戏的开发 - 骰宝</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-06-13T14:22:32+08:00">
                2018-06-13
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/06/13/赌博小游戏的开发-骰宝-py/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2018/06/13/赌博小游戏的开发-骰宝-py/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>visitors
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>去过赌场的朋友应该见过这个 —- 骰宝（Sic Bo）<br><a id="more"></a></p>
<p><img src="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/002.jpg?raw=true" alt=""><br>骰子的英文是 dice，有devil’s bonesdice的说法，所以google dice的时候会出现很多这种人型dice~<br><img src="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/003.png?raw=true" alt=""><br>下面是骰宝的台子，上面几乎涵盖所有的玩法：</p>
<ul>
<li>猜数字</li>
<li>猜单双</li>
<li>比大小</li>
<li>通杀</li>
<li><p>其他<br><img src="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/Sic_bo_Table.png?raw=true" alt=""><br>之前帮朋友做了下这个游戏的调研开发，觉得不难。<br>前提是了解其中的规则，<a href="https://baike.baidu.com/item/骰宝/5449340?fr=aladdin" target="_blank" rel="noopener">参考规则</a></p>
</li>
<li><p>下面的代码里，是站在庄家的角度写的</p>
</li>
<li>money为正的时候，庄家赚钱；money为负的时候，庄家亏钱</li>
</ul>
<p>Talk is cheap, show me the code.<br>直接上代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br></pre></td><td class="code"><pre><span class="line">import numpy as np</span><br><span class="line">import matplotlib.pyplot as plt  </span><br><span class="line">import itertools</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def cumsum(L):</span><br><span class="line">    #if L&apos;s length is equal to 1</span><br><span class="line">    if L[:-1] == []:</span><br><span class="line">        return L</span><br><span class="line">        </span><br><span class="line">    ret = cumsum(L[:-1])</span><br><span class="line">    ret.append(ret[-1] + L[-1])</span><br><span class="line">    return ret</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">chips = 0    # 玩家的筹码量初始值设置</span><br><span class="line">#spend_chips = 0</span><br><span class="line">money_ALL = []   # 每局的盈亏金额 = 庄家的筹码量</span><br><span class="line">money_accum_ALL = []</span><br><span class="line">ROUND_ALL = []</span><br><span class="line"></span><br><span class="line">for ii in range(1,11):</span><br><span class="line"></span><br><span class="line">    money = []   # 每局的盈亏金额 = 庄家的筹码量</span><br><span class="line">    money_accum = []</span><br><span class="line">    ROUND = []</span><br><span class="line"></span><br><span class="line">    for i in range(1,801):</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        # 假设每轮的赔率都是固定不变的 要变化的话就自动设置</span><br><span class="line">        </span><br><span class="line">        ROUND.append(i)</span><br><span class="line">        </span><br><span class="line">        #-----------------------------------------------------</span><br><span class="line">        #玩家买柱</span><br><span class="line">        #-----------------------------------------------------</span><br><span class="line">        #chips = chips + 10000       # 累计增加筹码</span><br><span class="line"></span><br><span class="line">        chips = 10000                # 每局开始有10000筹码</span><br><span class="line">        </span><br><span class="line">    #   rest_chips = chips - spend_chips</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        # 买大小</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_big_small = np.random.randint(0,chips+1) * is_buy</span><br><span class="line">        odds_big_small = 1                                       # 赔率设置</span><br><span class="line">        </span><br><span class="line">        if chips_big_small &gt; 0:</span><br><span class="line">            guess_big_small = np.random.randint(0,2)             # 0:猜小  1:猜大</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        # 买单双</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_odd_even = np.random.randint(0,chips-chips_big_small+1) * is_buy</span><br><span class="line">        odds_odd_even = 1                                        # 赔率设置</span><br><span class="line">        </span><br><span class="line">        if chips_odd_even &gt; 0:</span><br><span class="line">            guess_odd_even = np.random.randint(0,2)              # 0:猜双  1:猜单</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        #买三个一样数字（任意三个一样就行）</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_num = np.random.randint(0,chips-chips_big_small-chips_odd_even+1) * is_buy</span><br><span class="line">        if chips_num &gt; 0:</span><br><span class="line">            guess_A = np.random.randint(1,7)</span><br><span class="line">            guess_B = guess_A</span><br><span class="line">            guess_C = guess_A</span><br><span class="line">            </span><br><span class="line">        #买 1 1 1</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_num_1 = np.random.randint(0,chips-chips_big_small-chips_odd_even-chips_num+1) * is_buy</span><br><span class="line">        if chips_num_1 &gt; 0:</span><br><span class="line">            guess_A = 1</span><br><span class="line">            guess_B = guess_A</span><br><span class="line">            guess_C = guess_A</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        #买 2 2 2</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_num_2 = np.random.randint(0,chips-chips_big_small-chips_odd_even-chips_num-chips_num_1+1) * is_buy</span><br><span class="line">        if chips_num_2 &gt; 0:</span><br><span class="line">            guess_A = 2</span><br><span class="line">            guess_B = guess_A</span><br><span class="line">            guess_C = guess_A</span><br><span class="line">        </span><br><span class="line">        #买 3 3 3</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_num_3 = np.random.randint(0,chips-chips_big_small-chips_odd_even-chips_num-chips_num_1-chips_num_2+1) * is_buy</span><br><span class="line">        if chips_num_3 &gt; 0:</span><br><span class="line">            guess_A = 3</span><br><span class="line">            guess_B = guess_A</span><br><span class="line">            guess_C = guess_A</span><br><span class="line">        </span><br><span class="line">        #买 4 4 4</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_num_4 = np.random.randint(0,chips-chips_big_small-chips_odd_even-chips_num-chips_num_1-chips_num_2-chips_num_3+1) * is_buy</span><br><span class="line">        if chips_num_4 &gt; 0:</span><br><span class="line">            guess_A = 4</span><br><span class="line">            guess_B = guess_A</span><br><span class="line">            guess_C = guess_A</span><br><span class="line">            </span><br><span class="line">        #买 5 5 5</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_num_5 = np.random.randint(0,chips-chips_big_small-chips_odd_even-chips_num-chips_num_1-chips_num_2-chips_num_3-chips_num_4+1) * is_buy</span><br><span class="line">        if chips_num_5 &gt; 0:</span><br><span class="line">            guess_A = 5</span><br><span class="line">            guess_B = guess_A</span><br><span class="line">            guess_C = guess_A</span><br><span class="line">        </span><br><span class="line">        #买 6 6 6</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_num_6 = np.random.randint(0,chips-chips_big_small-chips_odd_even-chips_num-chips_num_1-chips_num_2-chips_num_3-chips_num_4-chips_num_5+1) * is_buy</span><br><span class="line">        if chips_num_6 &gt; 0:</span><br><span class="line">            guess_A = 6</span><br><span class="line">            guess_B = guess_A</span><br><span class="line">            guess_C = guess_A</span><br><span class="line">        </span><br><span class="line">        # 买总和数</span><br><span class="line">        is_buy = np.random.randint(0,2)</span><br><span class="line">        chips_sum_num = np.random.randint(0,chips-chips_big_small-chips_odd_even-chips_num-chips_num_1-chips_num_2-chips_num_3-chips_num_4-chips_num_5-chips_num_6+1) * is_buy</span><br><span class="line">        </span><br><span class="line">        if chips_sum_num &gt; 0:</span><br><span class="line">            guess_sum_num = np.random.randint(4,18)</span><br><span class="line">            </span><br><span class="line">            if guess_sum_num in [4,17]:                          # 赔率设置</span><br><span class="line">                odds_sum_num = 50</span><br><span class="line">            elif guess_sum_num in [5,16]:</span><br><span class="line">                odds_sum_num = 18</span><br><span class="line">            elif guess_sum_num in [6,15]:</span><br><span class="line">                odds_sum_num = 14</span><br><span class="line">            elif guess_sum_num in [7,14]:</span><br><span class="line">                odds_sum_num = 12</span><br><span class="line">            elif guess_sum_num in [8,13]:</span><br><span class="line">                odds_sum_num = 8</span><br><span class="line">            elif guess_sum_num in [9,10,11,12]:</span><br><span class="line">                odds_sum_num = 6</span><br><span class="line">        </span><br><span class="line">        # 全围</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        #-----------------------------------------------------</span><br><span class="line">        # 开局</span><br><span class="line">        #-----------------------------------------------------</span><br><span class="line">        A = np.random.randint(1,7)</span><br><span class="line">        B = np.random.randint(1,7)</span><br><span class="line">        C = np.random.randint(1,7)</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        #-----------------------------------------------------</span><br><span class="line">        # 检验结果</span><br><span class="line">        #-----------------------------------------------------</span><br><span class="line">    </span><br><span class="line">        money_sum_num = 0</span><br><span class="line">        money_num = 0</span><br><span class="line">        money_big_small = 0 </span><br><span class="line">        money_odd_even = 0</span><br><span class="line">        kill_money = 0</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">        # 通杀情况1</span><br><span class="line">        if chips_sum_num &gt; 0:</span><br><span class="line">    </span><br><span class="line">            result_sum_num = A+B+C</span><br><span class="line">            </span><br><span class="line">            if A == B &amp; B == C:                                               # 通杀情况下</span><br><span class="line">                if guess_sum_num == result_sum_num &amp; guess_sum_num not in [3,18]:            # 闲家猜对了</span><br><span class="line">                    money_sum_num = -(chips_sum_num * 24)</span><br><span class="line">                else:                                                                        # 猜错了 </span><br><span class="line">                    kill_money = chips                                       # 通杀，完毕</span><br><span class="line">                              </span><br><span class="line">                    </span><br><span class="line">        # 通杀情况2            </span><br><span class="line">        elif chips_num &gt; 0:</span><br><span class="line">            if A == B &amp; B == C:                                           # 通杀情况下</span><br><span class="line">                if guess_A == A:                                                            # 闲家猜中</span><br><span class="line">                    money_num = -(chips_num * 24)</span><br><span class="line">               # else:</span><br><span class="line">                    #kill_money = chips</span><br><span class="line">        </span><br><span class="line">        elif chips_num_1 &gt; 0:</span><br><span class="line">            if A == B &amp; B == C &amp; B == 1 :                                           # 通杀情况下</span><br><span class="line">                if guess_A == A:                                                            # 闲家猜中</span><br><span class="line">                    money_num = -(chips_num_1 * 150)</span><br><span class="line">                else:</span><br><span class="line">                    kill_money = chips            </span><br><span class="line">                  </span><br><span class="line">        elif chips_num_2 &gt; 0:</span><br><span class="line">            if A == B &amp; B == C &amp; B == 2 :                                           # 通杀情况下</span><br><span class="line">                if guess_A == A:                                                            # 闲家猜中</span><br><span class="line">                    money_num = -(chips_num_1 * 150)</span><br><span class="line">                else:</span><br><span class="line">                    kill_money = chips </span><br><span class="line">        elif chips_num_3 &gt; 0:</span><br><span class="line">            if A == B &amp; B == C &amp; B == 3 :                                           # 通杀情况下</span><br><span class="line">                if guess_A == A:                                                            # 闲家猜中</span><br><span class="line">                    money_num = -(chips_num_1 * 150)</span><br><span class="line">                else:</span><br><span class="line">                    kill_money = chips </span><br><span class="line">        elif chips_num_4 &gt; 0:</span><br><span class="line">            if A == B &amp; B == C &amp; B == 4  :                                           # 通杀情况下</span><br><span class="line">                if guess_A == A:                                                            # 闲家猜中</span><br><span class="line">                    money_num = -(chips_num_1 * 150)</span><br><span class="line">                else:</span><br><span class="line">                    kill_money = chips </span><br><span class="line">        elif chips_num_5 &gt; 0:</span><br><span class="line">            if A == B &amp; B == C &amp; B == 5 :                                           # 通杀情况下</span><br><span class="line">                if guess_A == A:                                                            # 闲家猜中</span><br><span class="line">                    money_num = -(chips_num_1 * 150)</span><br><span class="line">                else:</span><br><span class="line">                    kill_money = chips </span><br><span class="line">        </span><br><span class="line">        elif chips_num_6 &gt; 0:</span><br><span class="line">            if A == B &amp; B == C &amp; B == 6 :                                           # 通杀情况下</span><br><span class="line">                if guess_A == A:                                                            # 闲家猜中</span><br><span class="line">                    money_num = -(chips_num_1 * 150)</span><br><span class="line">                else:</span><br><span class="line">                    kill_money = chips </span><br><span class="line">       </span><br><span class="line">                    </span><br><span class="line">                    </span><br><span class="line">        # 买总和数   </span><br><span class="line">        if chips_sum_num &gt; 0:   </span><br><span class="line">            </span><br><span class="line">            if not (A == B &amp; B == C):</span><br><span class="line">                if guess_sum_num == result_sum_num:                             #如果猜中了</span><br><span class="line">                    money_sum_num = -(chips_sum_num*odds_sum_num)</span><br><span class="line">                else:</span><br><span class="line">                    money_sum_num = chips_sum_num</span><br><span class="line">        </span><br><span class="line">                </span><br><span class="line">                    </span><br><span class="line">    </span><br><span class="line">        </span><br><span class="line">        # 买大小</span><br><span class="line">        </span><br><span class="line">        if chips_big_small &gt; 0:</span><br><span class="line">            if A+B+C in list(range(1,11)):</span><br><span class="line">                result_big_small = 0  #结果为 小</span><br><span class="line">            else:</span><br><span class="line">                result_big_small = 1</span><br><span class="line">                </span><br><span class="line">            if guess_big_small == result_big_small:                               # 如果猜对了</span><br><span class="line">                money_big_small = -(chips_big_small*odds_big_small)               # 庄家给相应的钱</span><br><span class="line">            else:</span><br><span class="line">                money_big_small = chips_big_small</span><br><span class="line">        </span><br><span class="line">        # 买单双</span><br><span class="line">        </span><br><span class="line">        if chips_odd_even &gt; 0:</span><br><span class="line">            if (A+B+C)%2 == 1:     # 结果为 单</span><br><span class="line">                result_odd_even = 1</span><br><span class="line">            else:</span><br><span class="line">                result_odd_even = 0</span><br><span class="line">            </span><br><span class="line">            if guess_odd_even == result_odd_even:</span><br><span class="line">                money_odd_even = -(chips_odd_even*odds_odd_even)</span><br><span class="line">            else:</span><br><span class="line">                money_odd_even = chips_odd_even           </span><br><span class="line">                </span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        #-----------------------------------------------------</span><br><span class="line">        # 收钱/给钱</span><br><span class="line">        #-----------------------------------------------------</span><br><span class="line">    #    spend_chips = chips_big_small + chips_odd_even + chips_sum_num</span><br><span class="line">        </span><br><span class="line">        if kill_money == chips:</span><br><span class="line">            money.append(kill_money)</span><br><span class="line">        else:</span><br><span class="line">            money.append(money_big_small + money_odd_even + money_sum_num)</span><br><span class="line">            </span><br><span class="line">    money_ALL.append(money)</span><br><span class="line">        </span><br><span class="line">    money_accum = cumsum(money)</span><br><span class="line">    money_accum_ALL.append(money_accum)</span><br><span class="line">    </span><br><span class="line">    ROUND_ALL.append(ROUND)</span><br><span class="line">    </span><br><span class="line">#    plt.bar(ROUND,money)  </span><br><span class="line">#    plt.show() </span><br><span class="line">#    plt.bar(ROUND,money_accum)  </span><br><span class="line">#    plt.show() </span><br><span class="line">#    print(money)</span><br><span class="line"></span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">money_ALL = list(itertools.chain.from_iterable(money_ALL))</span><br><span class="line">money_accum_ALL = list(itertools.chain.from_iterable(money_accum_ALL))</span><br><span class="line">ROUND_ALL = list(itertools.chain.from_iterable(ROUND_ALL))</span><br><span class="line">ROUND_ALL = list(range(1,len(ROUND_ALL)+1))</span><br><span class="line"></span><br><span class="line">plt.bar(ROUND_ALL,money_ALL)  </span><br><span class="line">plt.show() </span><br><span class="line">plt.bar(ROUND_ALL,money_accum_ALL)  </span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure></p>
<p>给出结果图如下：<br><img src="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/010.png?raw=true" alt=""><br>这是每局的盈亏展示，可以看出在第4000局左右的时候，庄家大亏了一把<br><img src="https://github.com/YZHANG1270/Markdown_pic/blob/master/game_sic_bo/011.png?raw=true" alt=""><br>这是盈亏累积展示，在最终，8000局大战之后，还是庄家赢了15万左右</p>
<ul>
<li>不同国家，不同场子的玩法规则会有不一样，上面的code也不能包含所有规则</li>
<li>另外，python相关文件还是建议以英文命名，标题只是起到编程语言标识的作用</li>
<li>赌场还有很多其他游戏，如果你想败坏兴致，可以像我这么做🙃</li>
</ul>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>打赏2块钱，帮我买杯咖啡，继续创作，谢谢大家！☕~</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.png" alt="ZhangYi WeChat Pay"/>
        <p>WeChat Pay</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/python/" rel="tag"># python</a>
          
            <a href="/tags/game/" rel="tag"># game</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/06/01/XGBOOST使用说明/" rel="next" title="Machine Learning笔记 - XGBOOST 教程">
                <i class="fa fa-chevron-left"></i> Machine Learning笔记 - XGBOOST 教程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/07/10/参加CCF-GAIR-2018-的体验总结/" rel="prev" title="参加CCF-GAIR(2018)的体验总结">
                参加CCF-GAIR(2018)的体验总结 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-2691877571661707"
     data-ad-slot="1301633292"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpg"
                alt="ZhangYi" />
            
              <p class="site-author-name" itemprop="name">ZhangYi</p>
              <p class="site-description motion-element" itemprop="description">花时间做那些别人看不见的事~！</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">42</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">80</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/YZHANG1270" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:YZHANG1270@gmail.com" target="_blank" title="邮箱">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/p/1005053340707810?is_all=1" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i></a>
                  </span>
                
            </div>
          

          
          

          
          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ZhangYi</span>

  
</div>








  <div class="footer-custom">All content under <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/">CC BY-NC-ND 4.0</a></div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      visitors
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>


  


  
  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://codewithzhangyi.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://codewithzhangyi.com/2018/06/13/赌博小游戏的开发-骰宝-py/';
          this.page.identifier = '2018/06/13/赌博小游戏的开发-骰宝-py/';
          this.page.title = '赌博小游戏的开发 - 骰宝';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://codewithzhangyi.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  
  


  

  

</body>
</html>
